package sms

import (
	openapi "github.com/alibabacloud-go/darabonba-openapi/client"
	smsapi "github.com/alibabacloud-go/dysmsapi-20170525/v2/client"
	"github.com/alibabacloud-go/tea/tea"
	"sync"
)

var once sync.Once
var defaultSMS *sms

// Init 初始化默认SMS
func Init(smsConfig ISMSConfig) error {
	var err error
	once.Do(func() {
		defaultSMS, err = New(smsConfig)
	})

	return err
}

// New 新建SMS
func New(smsConfig ISMSConfig) (*sms, error) {
	conf := &openapi.Config{
		AccessKeyId:     tea.String(smsConfig.GetAccessKeyId()),
		AccessKeySecret: tea.String(smsConfig.GetAccessKeySecret()),
		Endpoint:        tea.String("dysmsapi.aliyuncs.com"),
	}

	var err error
	sms := &sms{}
	sms.client, err = smsapi.NewClient(conf)

	return sms, err
}

// GetClient 获取默认SMS Client
func GetClient() *smsapi.Client {
	return defaultSMS.client
}

// SendSMS 使用默认SMS 发送信息
func SendSMS(sendVO ISendVO) (*smsapi.SendSmsResponse, error) {
	return defaultSMS.SendSMS(sendVO)
}
